import { _decorator, Component, Node, Label, Button, director, game } from 'cc';
import { GameManager } from '../GameManager';
import { SaveManager } from '../SaveManager';
const { ccclass, property } = _decorator;

@ccclass('UIMain')
export class UIMain extends Component {
    @property(Label)
    public Timer: Label
    @property(Button)
    public QuitBtn:Button

    private curTimer:number = 0



    start() 
    {
        this.QuitBtn.node.on(Button.EventType.CLICK, this.OnClickBtn, this);
    }

    SetTimer(nTime:number)
    {
        this.curTimer = nTime
    }

    update(deltaTime: number)
    {
        if (GameManager.instance.IsGamePause > 0) {
            return
        }
        this.curTimer -= deltaTime
        if (this.curTimer > 0) {
            let min = Math.floor(this.curTimer / 60)
            let sec = Math.floor(this.curTimer % 60)
            this.Timer.string = min + ":" + sec
        }
        else //时间结束
        {
            GameManager.instance.TimeOut()
        }
    }

    OnClickBtn(btn:Button)
    {
        let btnName = btn.node.name
        console.log(btnName)
        if (btnName == "QuitBtn") {
            SaveManager.instance.Save()
            console.error("退出游戏")
            console.log(director.isPaused())
            if (director.isPaused()) {
                director.resume()
            }
            else {
                director.pause()
            }
            // game.end()//编辑器下调用会导致editor奔溃
        }
    }
}


